#include <header.h>

/**
 * 测试一下主线程打开文件以后，
 * 子线程能否通过文件描述符访问同一个文件对象？
 * 如果某个线程执行close之后会怎么样？
*/

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
void *run(void *arg){
	long fd = (long)arg;
	printf("open fd : %d\n", fd);
	pthread_mutex_lock(&lock);
	close(fd);
	pthread_mutex_unlock(&lock);
	return NULL;
}


int main(void){
	int fd = open("1.txt", O_CREAT | O_TRUNC | O_WRONLY, 0666);

	pthread_t thread_id;
	pthread_create(&thread_id, NULL, run, (void*)fd);
	
	sleep(2);
	pthread_mutex_lock(&lock);
	ssize_t bytes =  write(fd, "hello\n", 6);
	pthread_mutex_unlock(&lock);
	ERROR_CHECK(bytes, -1, "🤣 👉 🤡");

	pthread_join(thread_id, NULL);
	return 0;
}